package com.another.bidservice.controller;

import com.another.bidservice.entity.TUser;
import com.another.bidservice.service.IUserService;
import com.another.common.utils.common.MyException;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.another.common.utils.result.Result;
import com.another.common.utils.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    private Logger logger=Logger.getLogger(this.getClass());

    @Autowired
    private IUserService iUserService;

    @Autowired
    protected HttpServletRequest request;

    @PostMapping(value = "/getList")
    @ResponseBody
    public Result getList(@RequestBody TUser tUser) {
       try {
            return iUserService.getList(tUser);
        } catch (MyException me) {
            logger.info(me.getMessage(),me);
            return Result.error(me.getCode(), me.getMessage());
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
            return Result.error(ResultEnum.UNKONW_ERROR.getCode(), ResultEnum.UNKONW_ERROR.getMsg());
        }
    }

    @PostMapping(value = "/deleteList")
    @ResponseBody
    public Result deleteList(@RequestBody List<Integer> rowids) {
        try {
            return iUserService.deleteList(rowids);
        } catch (MyException me) {
            logger.info(me.getMessage(),me);
            return Result.error(me.getCode(), me.getMessage());
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
            return Result.error(ResultEnum.UNKONW_ERROR.getCode(), ResultEnum.UNKONW_ERROR.getMsg());
        }
    }

    @PostMapping(value = "/insertList")
    @ResponseBody
    public Result insertList(@RequestBody List<TUser> tUsers) {
        try {
            return iUserService.insertList(tUsers);
        } catch (MyException me) {
            logger.info(me.getMessage(),me);
            return Result.error(me.getCode(), me.getMessage());
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
            return Result.error(ResultEnum.UNKONW_ERROR.getCode(), ResultEnum.UNKONW_ERROR.getMsg());
        }
    }

    @PostMapping(value = "/updateList")
    @ResponseBody
    public Result updateList(@RequestBody List<TUser> tUsers) {
        try {
            return iUserService.updateList(tUsers);
        } catch (MyException me) {
            logger.info(me.getMessage(),me);
            return Result.error(me.getCode(), me.getMessage());
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
            return Result.error(ResultEnum.UNKONW_ERROR.getCode(), ResultEnum.UNKONW_ERROR.getMsg());
        }
    }
    
    @PostMapping(value = "/updateUser")
    @ResponseBody
    public Result updateUser(@RequestBody TUser tUser) {
        try {
            return iUserService.updateUser(tUser);
        } catch (MyException me) {
            logger.info(me.getMessage(),me);
            return Result.error(me.getCode(), me.getMessage());
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
            return Result.error(ResultEnum.UNKONW_ERROR.getCode(), ResultEnum.UNKONW_ERROR.getMsg());
        }
    }

    /**
     * 新增用户
     * @param tUser
     * @return
     */
    @PostMapping(value = "/insertUser")
    @ResponseBody
    public Result insertUser(@RequestBody TUser tUser) {
        try {
            return iUserService.insertUser(tUser);
        } catch (MyException me) {
            logger.info(me.getMessage(),me);
            return Result.error(me.getCode(), me.getMessage());
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
            return Result.error(ResultEnum.UNKONW_ERROR.getCode(), ResultEnum.UNKONW_ERROR.getMsg());
        }
    }

    @PostMapping(value = "/deleteUser")
    @ResponseBody
    public Result deleteUser(@RequestBody TUser tUser) {
        try {
            return iUserService.deleteUser(tUser);
        } catch (MyException me) {
            logger.info(me.getMessage(),me);
            return Result.error(me.getCode(), me.getMessage());
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
            return Result.error(ResultEnum.UNKONW_ERROR.getCode(), ResultEnum.UNKONW_ERROR.getMsg());
        }
    }


    /**
     * @author Another
     * @description :  web端登陆
     * @date 2020-3-17
     * @param tUser
     * @return com.another.exhibitioncommon.utils.result.Result
     * @version : 1.0
     */
    @PostMapping(value = "/webLogin")
    @ResponseBody
    public Result webLogin(@RequestBody TUser tUser) {
        try {
            if (StringUtils.isBlank(tUser.getPassword())) {
                return Result.error(ResultEnum.ERROR.getCode(), "用户名或密码为空！");
            }

            Result result = iUserService.webLogin(tUser, request);
//            String source = request.getHeader(sourceKey);

            return result;

        } catch (MyException me) {
            logger.info(me.getMessage(),me);
            return Result.error(me.getCode(), me.getMessage());
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
            return Result.error(ResultEnum.UNKONW_ERROR.getCode(), ResultEnum.UNKONW_ERROR.getMsg());
        }
    }
}



